package com.stagepayserver.payserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.stagepayserver.payserver.config.AlipayConfig;
import com.stagepayserver.payserver.pojo.QrCodeResponse;
import com.stagepayserver.payserver.pojo.QrResponse;
import com.stagepayserver.payserver.utils.QRCodeUtil;
import com.stagecommon.common.pojo.RespBean;
import com.stagepayserver.payserver.service.AlipayService;
import com.stagecommon.common.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: yxl
 * @since: 2022-01-04
 **/
@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayConfig config;

    private JSONObject jsonObject = new JSONObject();

    private Map<String, Object> hashMap = new ConcurrentHashMap<>();

    @Override
    public RespBean<byte[]> aliPay(PayVo payVo) throws Exception {
       jsonObject.put("type",payVo.getType());
        AlipayTradePrecreateModel alipayTradePrecreateModel = new AlipayTradePrecreateModel();
        alipayTradePrecreateModel.setOutTradeNo(payVo.getPayTradeNum());
        alipayTradePrecreateModel.setSubject(payVo.getSubject());
        alipayTradePrecreateModel.setTotalAmount(payVo.getTotalPrice());
        alipayTradePrecreateModel.setTimeoutExpress("30m");
        alipayTradePrecreateModel.setBody(jsonObject.toJSONString());
        QrCodeResponse qrCodeResponse = qrCodeResponse(alipayTradePrecreateModel);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        String logoPath = ResourceUtils.getFile("classpath:小黄鸡.png").getAbsolutePath();
        BufferedImage buffImg = QRCodeUtil.encode(qrCodeResponse.getQr_code(), logoPath, false);//获取二维码
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
        ImageIO.write(buffImg, "JPEG", imageOut);
        imageOut.close();
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        return RespBean.operationSuccess("创建成功",200,FileCopyUtils.copyToByteArray(input));
    }

    private QrCodeResponse qrCodeResponse(AlipayTradePrecreateModel model) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();
        AlipayTradePrecreateRequest alipayTradePrecreateRequest = new AlipayTradePrecreateRequest();
        alipayTradePrecreateRequest.setBizModel(model);
        alipayTradePrecreateRequest.setNotifyUrl(AlipayConfig.notify_url);
        alipayTradePrecreateRequest.setReturnUrl(AlipayConfig.return_url);
        AlipayTradePrecreateResponse execute = alipayClient.execute(alipayTradePrecreateRequest);
        QrResponse qrResponse = JSONObject.parseObject(execute.getBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_preCreate_response();
    }


    private AlipayClient alipayClient(){
        return new DefaultAlipayClient(AlipayConfig.gatewayUrl,AlipayConfig.app_id,AlipayConfig.merchant_private_key,
                "json",AlipayConfig.charset,AlipayConfig.alipay_public_key,AlipayConfig.sign_type);
    }
}
